﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace appLibrary
{
    public class MergeCell
    {
        public void setMergeCell(object sender, DataGridViewCellPaintingEventArgs e, ref DataGridView dvg, int col1)
        {



            if ((e.ColumnIndex == col1) && e.RowIndex != -1)
            {

                Brush gridBrush = new SolidBrush(dvg.GridColor);
                Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                Pen gridLinePen = new Pen(gridBrush);
                // Clear cell  
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Draw line (bottom border and right border of current cell)  
                //If next row cell has different content, only draw bottom border line of current cell
                if (e.RowIndex < dvg.Rows.Count - 1 && dvg.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                }

                // Draw right border line of current cell  
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                // draw/fill content in current cell, and fill only one cell of multiple same cells  
                if (e.Value != null)
                {
                    if (e.RowIndex > 0 && dvg.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())
                    {

                    }
                    else
                        e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);

                }

                e.Handled = true;

            }
        }

        public void setMergeCell(object sender, DataGridViewCellPaintingEventArgs e, ref DataGridView dvg, int[] cols ,  ref bool flat)
        {


                for (int i = 0; i < cols.Length; i++)
                {


                    if ((e.ColumnIndex == cols[i]) && e.RowIndex != -1)
                    {

                        Brush gridBrush = new SolidBrush(dvg.GridColor);
                        Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                        Pen gridLinePen = new Pen(gridBrush);
                        // Clear cell  
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        // Draw line (bottom border and right border of current cell)  
                        //If next row cell has different content, only draw bottom border line of current cell
                        if (e.RowIndex < dvg.Rows.Count - 1 && dvg.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value!=null && e.Value!=null && dvg.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
                        {
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        }
                        if (e.RowIndex < dvg.Rows.Count - 1 &&  (!(dvg.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null) || !(e.Value != null)))
                        {
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                        }

                        // Draw right border line of current cell  
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                        // draw/fill content in current cell, and fill only one cell of multiple same cells  
                        if (e.Value != null  )
                        {
                            if (e.RowIndex > 0 && dvg.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value!=null && e.Value!=null && dvg.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString())
                            {

                            }
                            else
                                if(e.ColumnIndex!=0 && e.ColumnIndex!=1)
                                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + e.CellBounds.Width / 2, e.CellBounds.Y + e.CellBounds.Height/2, StringFormat.GenericDefault);
                                else
                                //    Log.eLog("uknown",e.CellStyle.BackColor.Name+","+Color.DarkBlue.Name);
                                if (e.CellStyle.BackColor.Name.Equals("PowderBlue"))
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);

                                    else
                                        e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + e.CellBounds.Height / 2, StringFormat.GenericDefault);
                        }

                        e.Handled = true;

                    }
                }
            

            flat = false;
                


        }
    }
}
